import axios from "axios";
import router from "@/router";

import { ElMessage } from "element-plus";
import globalConfig from "@/utils/config";

let pending = [];
const CancelToken = axios.CancelToken;
let removePending = (config) => {
  for (let p in pending) {
    if (pending[p].u === config.url + "&" + config.method + "&" + config.data) {
      pending[p].f();
      pending.splice(p, 1);
    }
  }
};
const http = axios.create({
  baseURL: "", //globalConfig.baseURL + 'api/',
  timeout: 180000, // 请求超时时间
  headers: {
    "Content-Type": "application/json;charset=UTF-8",
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Headers": "X-Requested-With,Content-Type",
    "Access-Control-Allow-Methods": "POST,GET,OPTIONS",
  },
});

// request拦截器
http.interceptors.request.use(
  (config) => {
    removePending(config);
    config.cancelToken = new CancelToken((c) => {
      pending.push({ u: config.url + "&" + config.method, f: c });
    });

    let token = sessionStorage.getItem("token");
    if (token) {
      config.headers.token = token;
    } else {
      config.headers.token = "";
    }
    if(sessionStorage.getItem("isOrgName") !='指挥中心'){
      config.headers.token = sessionStorage.getItem("token");
    }
    if( config.url.split('/')[config.url.split('/').length - 1] == 'BaseInfo'){
        config.headers.token = sessionStorage.getItem("token");
    }
    // let bmToken = sessionStorage.getItem("Bmtoken");

    // if (config.url.split("/")[config.url.split("/").length - 1] == "BaseInfo") {
    //   config.headers.token = bmToken;
    // }
    return config;
  },
  (error) => {
    // if (error) {
    // 	ElMessage({
    // 		ElMessage: error,
    // 		type: 'error'
    // 	});
    // }
  }
);

http.interceptors.response.use(
  function (response) {
    removePending(response.config);
    if (response.status === 200) {
      if (response.data.code == 10) {
        ElMessage({
          message: response.data.msg,
          type: "error",
        });
        if (sessionStorage.getItem("token")) {
          sessionStorage.clear();
          location.reload()
        }
        // if (!sessionStorage.getItem("token")) {
        //   sessionStorage.clear();
        //   router.push("/");
        //   setTimeout(()=>{
        //     location.reload();

        //   },500)
        // }
        return response.data;
      }
      if (response.data.code == null) return response;
      if (response.data.code === 0) {
        return response.data;
      }

      if (response.data.code == 1) {
        ElMessage({
          message: response.data.message,
          type: "error",
        });
        return response.data;
      }
      //  else if (response.data.code == 1) {
      //   if (response.config.url == globalConfig.baseURL + "api/base/Login") {
      //     if (response.data.Message == "账号或密码错误，请重试。") {
      //       ElMessage({
      //         message: response.data.msg,
      //         type: "error",
      //       });
      //     } else {
      //       ElMessage({
      //         message: response.data.msg,
      //         type: "warning",
      //       });
      //     }
      //   } else {
      //     if (response.data.Message && response.data.Message != "") {
      //       ElMessage({
      //         message: response.data.Message,
      //         type: "error",
      //       });
      //     } else {
      //       ElMessage({
      //         message: "",
      //         type: "error",
      //       });
      //     }
      //   }
      //   return response.data;
      // } else if (response.data.code) {
      //   if (response.data.Message && response.data.Message != "") {
      //     ElMessage({
      //       message: response.data.Message,
      //       type: "error",
      //     });
      //   } else {
      //     ElMessage({
      //       message: "数据请求错误",
      //       type: "error",
      //     });
      //   }
      //   return response.data;
      // }
    }
  },
  function (error) {
    if (error && error != "Cancel") {
      ElMessage({
        ElMessage: error,
        type: "error",
      });
    }
  }
);

export default http;
